package com.baidu.infinity.ui.fragment.home.shape.shapes

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.os.Build
import androidx.annotation.RequiresApi
import com.baidu.infinity.ui.fragment.home.shape.BaseShape
import kotlin.math.abs
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.min
import kotlin.math.sin

class LocationShape: BaseShape() {
    private var angle: Float = 0f
    private var length: Double = 0.0

    @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
    override fun draw(canvas: Canvas) {
        super.draw(canvas)
        mPaint.style = Paint.Style.STROKE
        length = Math.min(abs(endY - endX), abs(startY - startX)).toDouble() / 6
        mPath.reset()
        setArrowLineShape(mPath,startX,centerY,endX,centerY)
        canvas.drawPath(mPath,mPaint)
        mPath.reset()
        setArrowLineShape(mPath,centerX,endY,centerX,startY)
        canvas.drawPath(mPath,mPaint)
    }

    override fun containsPoint(x: Float, y: Float): Boolean {
        return super.containsPoint(x, y)
    }
    private fun setArrowLineShape(path: Path, startX: Float, startY: Float, endX: Float, endY: Float){
        path.moveTo(startX, startY)
        path.lineTo(endX, endY)
        angle = atan2(startY - endY, startX - endX)
        path.lineTo((endX + length * cos(angle - Math.toRadians(30.0))).toFloat(), (endY + length * sin(angle - Math.toRadians(30.0))).toFloat())
        path.moveTo(endX, endY)
        path.lineTo((endX + length * cos(angle + Math.toRadians(30.0))).toFloat(), (endY + length * sin(angle + Math.toRadians(30.0))).toFloat())
    }
}